package com.haredot.holder;

import com.haredot.entity.UserPrincipal;

/**
 * 令牌上下文持有者
 */
public class TokenContextHolder {

    private static ThreadLocal<UserPrincipal> threadLocal = new ThreadLocal<>();

    public static void setToken(String token, Long userId) {
        threadLocal.set(new UserPrincipal(userId, token));
    }

    public static String getToken() {
        return threadLocal.get() != null ? threadLocal.get().getName() : null ;
    }

    public static Long getCurrentUserId() {
        return threadLocal.get() != null ? threadLocal.get().getId() : null;
    }

    public static void remove() {
        threadLocal.remove();
    }
}
